import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test04 {
    public static void main(String[] args) throws InterruptedException {
        final int numThreads = 20;
        CountDownLatch latch = new CountDownLatch(numThreads);
        // 创建一个线程池
        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        // 提交任务到线程池
        for (int i = 0; i < numThreads; i++) {
            final int threadNumber = i;
            executor.submit(() -> {
                // 子线程打印编号
                System.out.println(threadNumber);
                // 完成任务后减小计数器
                latch.countDown();
            });
        }
        // 主线程等待所有子线程完成
        latch.await();
        // 所有子线程完成后执行
        System.out.println("ok");
        // 关闭线程池
        executor.shutdown();
    }
}



